package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.OtherParty;
import com.mohe.dangjian.service.interfaces.IOtherParty;
import com.mohe.dangjian.utils.Page;

public class OtherPartyImpl implements IOtherParty {
	
	private IDao<OtherParty> otherPartyDao;

	public IDao<OtherParty> getOtherPartyDao() {
		return otherPartyDao;
	}

	public void setOtherPartyDao(IDao<OtherParty> otherPartyDao) {
		this.otherPartyDao = otherPartyDao;
	}

	@Override
	public List<OtherParty> getOtherPartyList(OtherParty otherParty, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from OtherParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 单位名称
		if (!StringUtils.isBlank(otherParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + otherParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(otherParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + otherParty.getAddress() + "%%");
		}
		// 所属片区
		if (!StringUtils.isBlank(otherParty.getArea())) {
			where.append(" and area like ?");
			params.add("%%" + otherParty.getArea() + "%%");
		}
		// 单位性质
		if (!StringUtils.isBlank(otherParty.getType())) {
			where.append(" and type like ?");
			params.add("%%" + otherParty.getType() + "%%");
		}
		// 单位员工数
		if (otherParty.getWorkerNum() != null) {
			where.append(" and workerNum = ?");
			params.add(otherParty.getWorkerNum());
		}
		// 是否建有党组织
		if (otherParty.getHasParty() != null) {
			where.append(" and hasParty = ?");
			params.add(otherParty.getHasParty());
		}
		// 党组织名称
		if (!StringUtils.isBlank(otherParty.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + otherParty.getPartyName() + "%%");
		}
		// 是否为区域化党建单位
		if (otherParty.getIsRegional() != null) {
			where.append(" and isRegional = ?");
			params.add(otherParty.getIsRegional());
		}
		// 党员数
		if (otherParty.getMemberNum() != null) {
			where.append(" and memberNum = ?");
			params.add(otherParty.getMemberNum());
		}
		// 单位负责人姓名
		if (!StringUtils.isBlank(otherParty.getLeaderName())) {
			where.append(" and leaderName like ?");
			params.add("%%" + otherParty.getLeaderName() + "%%");
		}
		// 负责人政治面貌
		if (!StringUtils.isBlank(otherParty.getLeaderPolitical())) {
			where.append(" and leaderPolitical like ?");
			params.add("%%" + otherParty.getLeaderPolitical() + "%%");
		}
		// 负责人电话
		if (!StringUtils.isBlank(otherParty.getLeaderTel())) {
			where.append(" and leaderTel like ?");
			params.add("%%" + otherParty.getLeaderTel() + "%%");
		}
		// 备注
		if (!StringUtils.isBlank(otherParty.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + otherParty.getNote() + "%%");
		}

		where.append(" order by createDate desc");
		List<OtherParty> list = otherPartyDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(OtherParty otherParty) {
		StringBuffer where = new StringBuffer();
		where.append("from OtherParty where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 单位名称
		if (!StringUtils.isBlank(otherParty.getName())) {
			where.append(" and name like ?");
			params.add("%%" + otherParty.getName() + "%%");
		}
		// 地址
		if (!StringUtils.isBlank(otherParty.getAddress())) {
			where.append(" and address like ?");
			params.add("%%" + otherParty.getAddress() + "%%");
		}
		// 所属片区
		if (!StringUtils.isBlank(otherParty.getArea())) {
			where.append(" and area like ?");
			params.add("%%" + otherParty.getArea() + "%%");
		}
		// 单位性质
		if (!StringUtils.isBlank(otherParty.getType())) {
			where.append(" and type like ?");
			params.add("%%" + otherParty.getType() + "%%");
		}
		// 单位员工数
		if (otherParty.getWorkerNum() != null) {
			where.append(" and workerNum = ?");
			params.add(otherParty.getWorkerNum());
		}
		// 是否建有党组织
		if (otherParty.getHasParty() != null) {
			where.append(" and hasParty = ?");
			params.add(otherParty.getHasParty());
		}
		// 党组织名称
		if (!StringUtils.isBlank(otherParty.getPartyName())) {
			where.append(" and partyName like ?");
			params.add("%%" + otherParty.getPartyName() + "%%");
		}
		// 是否为区域化党建单位
		if (otherParty.getIsRegional() != null) {
			where.append(" and isRegional = ?");
			params.add(otherParty.getIsRegional());
		}
		// 党员数
		if (otherParty.getMemberNum() != null) {
			where.append(" and memberNum = ?");
			params.add(otherParty.getMemberNum());
		}
		// 单位负责人姓名
		if (!StringUtils.isBlank(otherParty.getLeaderName())) {
			where.append(" and leaderName like ?");
			params.add("%%" + otherParty.getLeaderName() + "%%");
		}
		// 负责人政治面貌
		if (!StringUtils.isBlank(otherParty.getLeaderPolitical())) {
			where.append(" and leaderPolitical like ?");
			params.add("%%" + otherParty.getLeaderPolitical() + "%%");
		}
		// 负责人电话
		if (!StringUtils.isBlank(otherParty.getLeaderTel())) {
			where.append(" and leaderTel like ?");
			params.add("%%" + otherParty.getLeaderTel() + "%%");
		}
		// 备注
		if (!StringUtils.isBlank(otherParty.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + otherParty.getNote() + "%%");
		}

		return otherPartyDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveOtherParty(OtherParty otherParty) {
		return otherPartyDao.insert(otherParty);
	}

	@Override
	public boolean saveOtherPartyList(List<OtherParty> otherPartyList) {
		return otherPartyDao.insert(otherPartyList);
	}

	@Override
	public OtherParty getOtherPartyById(Long timeKey) {
		OtherParty otherParty = otherPartyDao.getByKey(timeKey);
		if (otherParty != null) {
			return otherParty;
		}
		return null;
	}

	@Override
	public boolean updateOtherParty(OtherParty otherParty) {
		return otherPartyDao.update(otherParty);
	}

	@Override
	public boolean delOtherParty(Long timeKey) {
		return otherPartyDao.delete(timeKey);
	}

	@Override
	public List<OtherParty> getAllOtherParty() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return otherPartyDao.query(where.toString(), null);
	}

}
